<?php

namespace app\home\controller;

use think\Controller;
use think\Loader;
use think\Request;

class User extends Controller
{
    public function add()
    {
        if (Request::instance()->isGet()){
            $this->assign('HomeAddErrorInfo', session('HomeAddErrorInfo'));
            return view();
        }
        if (Request::instance()->isPost()){
            $nickname = input('post.nickname');
            $email = input('post.email');
            $pwd = input('post.password');
            $tel = input('post.tel');
            (!$nickname) && $this->AddErrorRedirect('用户名不能为空');
            (!$pwd) && $this->AddErrorRedirect('密码不能为空');
            (!$email) && $this->AddErrorRedirect('邮箱不能为空');
            (!$tel) && $this->AddErrorRedirect('手机不能为空');

            $pwd =  password_hash($pwd,PASSWORD_DEFAULT);
            $this->checkAdd($nickname,$email,$pwd,$tel);
        }

        
    }
    public function AddErrorRedirect($error, $url = 'home/User/add'){
        session('HomeAddErrorInfo', $error);
        $this->redirect($url);
    }
    public function checkAdd($nickname,$email,$pwd,$tel){
        $AddInfo = Loader::model('User')->UserAdd($nickname,$email,$pwd,$tel);
        if ($AddInfo['status'] < 0){
            $this->AddErrorRedirect($AddInfo['msg']);
        }else{
            session('HomeAddErrorInfo', null);
            _frame_reload('注册成功','home/index/index');
        }
    }
    public function loginOut(){
        session('UserXinxi',null);
        _frame_reload('已退出', 'home/index/index');
    }
    public function userInfo(){
        $sg_ccolumn = Loader::model('commodity')->columnInfo();
        $this->assign('columnInfo',$sg_ccolumn);
        return view();
    }
    public function buyInfo(){
        return view();
    }
    public function evaluate(){
        $a =  input('post.pingjia');
        json($a);
        return view();
    }
    public function site(){
        $sg_ccolumn = Loader::model('commodity')->columnInfo();
        $this->assign('columnInfo',$sg_ccolumn);
        $uid = session('UserXinxi')['user']['uid'];
        if (empty($uid)){
            _frame_reload('请登录','home/user/Add',2);
        }
        $siteInfo = Loader::model('user')->siteInfo($uid);
        $this->assign('siteInfo',$siteInfo);

        return view();
    }
    public function siteAdd(){
        $xinxi = input('post.');
        $uid = session('UserXinxi')['user']['uid'];

        if (empty($uid)){
            _frame_reload('请登录','home/user/Add',2);
        }
        (!$xinxi['code']) && _frame_reload('邮编不能为空','home/user/site',2);
        (!$xinxi['tel']) && _frame_reload('手机号不能为空','home/user/site',2);
        (!$xinxi['address']) && _frame_reload('地址不能为空','home/user/site',2);
        (!$xinxi['name']) && _frame_reload('收货人姓名不能为空','home/user/site',2);
        if ($xinxi['shengfen'] === '请选择'){
            _frame_reload('请选择城市','home/user/site',2);
        }
        if (!checkZipcode($xinxi['code'])){
            _frame_reload('邮编格式不正确','home/user/site',2);
        }
        preg_match(config('MOBILE_PREG_MATCH'),$xinxi['tel'],$match);
        if(empty($match)){
            _frame_reload('手机格式错误!','home/user/site',2);
        }
        $data[] = [
            'region' => $xinxi['shengfen'] .' '. $xinxi['chengshi'].' '.$xinxi['qu'],
            'address' => $xinxi['address'],
            'code' => $xinxi['code'],
            'name' => $xinxi['name'],
            'tel'=>$xinxi['tel'],
            'uid'=>session('UserXinxi')['user']['uid']
        ];

        if (isset($xinxi['id']) && !empty($xinxi['id'])){
            $id = $xinxi['id'];
            $a = Loader::model('user')->siteEdit($id,$data['0']);
            if ($a){
                _frame_reloads('成功','',1);
            }
            _frame_reloads('失败','',2);
        }else{
            $a = Loader::model('user')->siteAdd($data['0']);
            if ($a){
                _frame_reload('成功','home/user/site',1);
            }
            _frame_reload('失败','home/user/site',2);
        }





    }
    public function siteEdit(){
        $id = input('get.id');
        $editInfo = Loader::model('user')->editInfo($id);
        $this->assign('editInfo',$editInfo);
        return view();
    }
    public function siteDel(){
        $id = input('post.id');
        if (!$id){
            return json(['status'=>-1,'msg'=>'请选择要删除的角色!']);
        }
        $siteDel = Loader::model('user')->siteDel($id);
        if ($siteDel){
            return json(['status'=>1,'msg'=>'删除成功']);
        }
        return json(['status'=>-1,'msg'=>'删除失败']);
    }
}
